﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Extensions.Configuration;

namespace News.CYT369.Com.Domains
{
    /// <summary>
    /// 系统配置服务
    /// </summary>
    public class SystemSettingService
    {
        #region init
        private static readonly SystemSettingService m_Instance = new SystemSettingService();
        private SystemSettingService()
        {

#if DEBUG
            string basePath = System.IO.Directory.GetCurrentDirectory();
            if (!System.IO.File.Exists(System.IO.Path.Combine(basePath, "appsettings.json")))
            {
                basePath = new System.IO.FileInfo(System.Reflection.Assembly.GetEntryAssembly().Location).DirectoryName;
            }
#else
            string basePath = new System.IO.FileInfo(System.Reflection.Assembly.GetEntryAssembly().Location).DirectoryName;
#endif
            var builder = new ConfigurationBuilder()
                .SetBasePath(basePath).AddJsonFile(@"appsettings.json", optional: true, reloadOnChange: true);

            Configuration = builder.Build();
        }

        public IConfigurationRoot Configuration { get; set; }

        /// <summary>
        /// 类型实例
        /// </summary>
        public static SystemSettingService Instance {
            get {
                return m_Instance;
            }
        }
        #endregion

        #region Connection
        public string DefaultConnection {
            get {
                return Configuration.GetSection("ConnectionStrings:DefaultConnection").Value;
            }
        }

        public string RedisConnection
        {
            get
            {
                return Configuration.GetSection("ConnectionStrings:RedisConnection").Value;
            }
        }
        #endregion

        #region SystemSetting
        public string SystemName {
            get {
                return Configuration.GetSection("SystemSetting:SystemName").Value;
            }
        }

        public string AppID {
            get {
                return Configuration.GetSection("SystemSetting:AppID").Value;
            }
        }

        public string AppKey {
            get {
                return Configuration.GetSection("SystemSetting:AppKey").Value;
            }
        }

        public string UserServiceURL
        {
            get
            {
                return Configuration.GetSection("SystemSetting:UserServiceURL").Value;
            }
        }

        public string UserCenterRegisterURL {
            get {
                return Configuration.GetSection("SystemSetting:UserCenterRegisterURL").Value;
            }
        }

        public string UserCenterLoginURL {
            get {
                return Configuration.GetSection("SystemSetting:UserCenterLoginURL").Value;
            }
        }

        public string UserCenterLogOffURL
        {
            get
            {
                return Configuration.GetSection("SystemSetting:UserCenterLogOffURL").Value;
            }
        }

        public string LoginReturnURL
        {
            get {
                return Configuration.GetSection("SystemSetting:LoginReturnURL").Value;
            }
        }


        public int PageSize {
            get {
                int id = 0;
                int.TryParse(Configuration.GetSection("SystemSetting:PageSize").Value, out id);
                if (id <= 0)
                {
                    id = 20;
                }
                return id;
            }
        }

        public string PageList {
            get {
                return Configuration.GetSection("SystemSetting:PageList").Value;
            }
        }

        /// <summary>
        /// 登录超时时间
        /// </summary>
        public int IdleTimeout {
            get {

                int n = 0;
                int.TryParse(Configuration.GetSection("SystemSetting:IdleTimeout").Value, out n);
                if (n <= 0)
                {
                    n = 120;
                }
                return n;
            }
        }


        /// <summary>
        /// 数据库类型
        /// </summary>
        public string DbType {
            get {
                return Configuration.GetSection("SystemSetting:DbType").Value;
            }
        }


        public int ReportReadStatusTime {
            get {

                int n = 0;
                int.TryParse(Configuration.GetSection("SystemSetting:ReportReadStatusTime").Value, out n);
                if (n <= 0)
                {
                    n = 15000;
                }
                return n;
            }
        }

        #endregion

        #region Aliyun.OSS
        private News.CYT369.Com.Domains.Aliyuns.EndpointParameter m_InternalBucket;
        public News.CYT369.Com.Domains.Aliyuns.EndpointParameter InternalBucket {
            get {
                if (m_InternalBucket == null)
                {
                    m_InternalBucket = new News.CYT369.Com.Domains.Aliyuns.EndpointParameter()
                    {
                        Endpoint = Configuration.GetSection("Aliyun.OSS:Internal").Value,
                        AccessKeyId = Configuration.GetSection("Aliyun.OSS:AccessKeyId").Value,
                        AccessKeySecret = Configuration.GetSection("Aliyun.OSS:AccessKeySecret").Value,
                        Bucket = Configuration.GetSection("Aliyun.OSS:Public.Bucket").Value,
                        HttpURL = Configuration.GetSection("Aliyun.OSS:Public.Bucket.HTTP").Value
                    };
                }

                return m_InternalBucket;
            }
        }


        //private News.CYT369.Com.Domains.Aliyuns.EndpointParameter m_InternalBucket2;
        //public News.CYT369.Com.Domains.Aliyuns.EndpointParameter InternalBucket2 {
        //    get {
        //        if (m_InternalBucket2 == null)
        //        {
        //            m_InternalBucket2 = new News.CYT369.Com.Domains.Aliyuns.EndpointParameter()
        //            {
        //                Endpoint = Configuration.GetSection("Aliyun.OSS2:Internal").Value,
        //                AccessKeyId = Configuration.GetSection("Aliyun.OSS2:AccessKeyId").Value,
        //                AccessKeySecret = Configuration.GetSection("Aliyun.OSS2:AccessKeySecret").Value,
        //                Bucket = Configuration.GetSection("Aliyun.OSS2:Public.Bucket").Value,
        //                HttpURL = Configuration.GetSection("Aliyun.OSS2:Public.Bucket.HTTP").Value
        //            };
        //        }

        //        return m_InternalBucket2;
        //    }
        //}
        #endregion
    }

}
